package com.five.wechat.serviceImp;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.five.common.been.ReceiveTextEntity;
import com.five.common.been.ReturnTextEntity;
import com.five.common.util.BaseUtil;
import com.five.common.util.XmlUtil;
import com.five.wechat.service.TextService;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

/**
 * @ClassName TestServiceType
 * @Author Administrator
 * @Date 2018/12/30 15:35
 * @Version 1.0
 * @Description 处理文本类型的消息
 **/
@Service("textServiceType")
public class TextServiceImp implements TextService {
    private static final String MENU = "功能";
    private static final String TEST = "0000";

    ObjectMapper mapper = new ObjectMapper();

    @Override
    public String handleAllText(Map<String, String> xml) throws IOException {
        String value = mapper.writeValueAsString(xml);
        ReceiveTextEntity receive = mapper.readValue(value, ReceiveTextEntity.class);

        String newTime = BaseUtil.getBaseUtil().getIntegerTime();
        ReturnTextEntity reply = new ReturnTextEntity();
        reply.setToUserName(receive.getFromUserName());
        reply.setFromUserName(receive.getToUserName());
        reply.setCreatetime(newTime);
        reply.setMsgType(receive.getMsgType());
        /**
         * 测试期间,dao模块和api接口模块未完工,数据写死,首先判断发送过来的content
         * 如果content是menu,去数据库查询menu的内容
         * 如果不是menu,是四位数字,根据四位数据查询绑定的内容
         * 否则返回不识别内容
         */
        if (MENU.equals(receive.getContent())) {
            reply.setContent("欢迎使用菜单功能,测试请输入0000");
        } else if (TEST.equals(receive.getContent())) {
            reply.setContent("测试成功");
        } else {
            reply.setContent("未识别内容");
        }
        return XmlUtil.getXmlUtil().ReturnMsg(reply);
    }
}
